package org.kong.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.kong.pojo.dto.UserDTO;
import org.kong.pojo.dto.UserLoginDTO;
import org.kong.pojo.entity.User;
import org.kong.pojo.vo.UserVO;
import org.kong.service.UserService;
import org.kong.utils.JwtUtils;
import org.kong.utils.RedisUtil;
import org.kong.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.springframework.security.authorization.AuthorityReactiveAuthorizationManager.hasAuthority;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/8/18
 * @desc
 */
@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:8080")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLoginDTO userLoginDTO) {
        // log.info("用户登录：{}", userLoginDTO);
        String token = userService.login(userLoginDTO);
        if (token != null) {
            return Result.success("登录成功", token);
        }
        return Result.error("登录失败");
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:user:list')")
    public Result<PageInfo> list(UserDTO userDTO) {
        // log.info("查询用户列表：{}", userDTO);
        PageHelper.startPage(userDTO.getPageNum(), userDTO.getPageSize());
        List<UserVO> list = userService.list(userDTO);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        log.info("查询用户列表：{}", pageInfo);
        if (list != null) {

            return Result.success("查询成功", pageInfo);
        }
        return Result.error("查询失败");
    }

    //通过id删除用户
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('system:user:del')")
    public Result<String> delete(Integer id) {
        // log.info("删除用户：{}", id);
        boolean b = userService.delete(id);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //通过ids删除用户
    @GetMapping("/deleteBatch")
    @PreAuthorize("hasAuthority('system:user:del')")
    public Result<String> deleteBatch(@RequestParam List<Integer> ids) {
        // log.info("删除用户：{}", ids);
        boolean b = userService.deleteBatch(ids);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //添加用户
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('system:user:add')")
    public Result<String> add(@RequestBody User user) {
        log.info("添加用户：{}", user);

        boolean b = userService.add(user);
        if (b) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    //修改用户
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('system:user:update')")
    public Result<String> update(@RequestBody User user) {
        log.info("修改用户：{}", user);
        boolean b = userService.update(user);
        if (b) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 系统用户退出
     */
    @PostMapping("/user/logout")
    public Result<String> logout(String token) {
        Claims claims = jwtUtils.parseJWT(token);
        Object uuid = claims.get("uuid");
        // 清除redis中的令牌
        redisUtil.deleteObject("user:key:"+uuid);
        return Result.success("退出成功");
    }
}
